﻿Imports OMANE.Common.UI

Public Class UIHelper

    Shared DefaultTitleIterator As Integer
    Dim UIService As IUIService


    Sub New(ByVal uiservice As IUIService)

        Me.UIService = uiservice

    End Sub

    Sub CreateNavigationTreeFromXml(ByVal x As XElement)
        DefaultTitleIterator = 0

        Dim rootnode = x.<NavigationTree>.FirstOrDefault()

        Dim rootnodeitem = New V2ViewModelTabGroup() With {.Title = GetTitle(rootnode.@Title)}

        For Each xelt In rootnode.<NavigationNode>

            CreateNavigationTreeNodeFromXml(xelt, rootnodeitem)

        Next

        UIService.V2ViewModelAccordeonMenu.Inventaires.Body.Add(rootnodeitem)

    End Sub

    Sub CreateNavigationTreeNodeFromXml(ByVal x As XElement, ByVal parentnode As V2ViewModelTabGroup)

        Dim nodeitem = New V2ViewModelTabGroup() With {.Title = GetTitle(x.@Title)}

        If x.Attributes("Type").Count() > 0 Then
            If parentnode.TabGroup Is Nothing Then
                parentnode.TabGroup = New ObjectModel.ObservableCollection(Of Common.UI.IViewModelTabSection)
            End If
            nodeitem.TabGroup = parentnode.TabGroup
            If String.IsNullOrEmpty(x.@Type) Then
                nodeitem.TabSection = New DefaultTabSection() With {.Title = nodeitem.Title}
                parentnode.TabGroup.Add(nodeitem.TabSection)
            Else
                nodeitem.TabSection = Me.GetType().Assembly.CreateInstance(x.@Type)
                nodeitem.TabSection.Title = nodeitem.Title
                parentnode.TabGroup.Add(nodeitem.TabSection)

                For Each xelt In x.<Property>
                    ComponentModel.TypeDescriptor.GetProperties(nodeitem.TabSection)(xelt.@Name).SetValue(nodeitem.TabSection, xelt.@Value)
                Next

            End If
        End If


        parentnode.Children.Add(nodeitem)

        For Each xelt In x.<NavigationNode>

            CreateNavigationTreeNodeFromXml(xelt, nodeitem)

        Next
    End Sub

    Sub CreateMainMenuFromXml(ByVal x As XElement)
        DefaultTitleIterator = 0

        Dim mainmenu = x.<MainMenu>.FirstOrDefault()

        Dim mainmenuitem = New ViewModelMainMenuItem() With {.Title = GetTitle(mainmenu.@Title)}

        For Each xelt In mainmenu.<Menu>

            CreateMainMenuNodeFromXml(xelt, mainmenuitem)

        Next

        UIService.ViewModelMainMenu.Children.Add(mainmenuitem)
    End Sub

    Function GetTitle(ByVal source As String) As String
        If String.IsNullOrEmpty(source) Then
            DefaultTitleIterator = DefaultTitleIterator + 1
            Return "Untitled_" & DefaultTitleIterator
        End If

        Return source
    End Function

    Sub CreateMainMenuNodeFromXml(ByVal x As XElement, ByVal parentmenu As ViewModelMainMenuItem)

        Dim menuitem = New ViewModelMainMenuItem() With {.Title = GetTitle(x.@Title)}

        parentmenu.Children.Add(menuitem)

        If x.<Menu>.Count() = 0 Then
            If x.Attributes("Name").Count() > 0 Then
                If Not String.IsNullOrEmpty(x.@Name) Then
                    menuitem.Command = New UICommand(Sub(obj) UIItemClick("mainmenu", x.@Name))
                End If
            End If
        Else
            For Each xelt In x.<Menu>

                CreateMainMenuNodeFromXml(xelt, menuitem)

            Next
        End If


    End Sub

    Sub UIItemClick(ByVal sourceType As String, ByVal sourceName As String)

        If sourceName = "new_pmt" Then

            'Dim VisuDetailFichier As New WindowDetailTbleFlux
            'VisuDetailFichier.Show()

            Dim NewPMTdialog As New NewPMTWizardDialog
            If NewPMTdialog.ShowDialog = True Then
                Dim NewPMT As IPMT = NewPMTdialog.Result
                Dim VMPMT As New ViewModelPMT(NewPMT) With {.Title = "PMT-" & _
                    AnneeMoisConverter.AnneeMoisToString_Simple(NewPMT.ParametresGeneraux.AnneeRealisation, _
                                                                NewPMT.ParametresGeneraux.MoisRealisation) _
                    & NewPMT.ParametresGeneraux.ComplementNom}
                PMTModule.RootNode.Children.Add(New OMANE.Common.UI.V2ViewModelTabGroup With {.Title = "PMT-" & _
                    AnneeMoisConverter.AnneeMoisToString_Simple(NewPMT.ParametresGeneraux.AnneeRealisation, _
                                                                NewPMT.ParametresGeneraux.MoisRealisation) _
                    & NewPMT.ParametresGeneraux.ComplementNom, .TabSection = VMPMT, .TabGroup = PMTModule.RootNode.TabGroup})
                PMTModule.RootNode.TabGroup.Add(VMPMT)

                PMTModule.Model.ListePMT.Add(NewPMT)
            End If
        End If

        'UIService.ShowMessage("sourceType: " & sourceType & " sourceName : " & sourceName)

        'Dim opd = New Microsoft.Win32.OpenFileDialog()
        '' opd.Filter = "xls"
        'If opd.ShowDialog() Then
        '    'Dim constring = "Dsn=Excel Files;dbq=" & opd.FileName & _
        '    '                ";defaultdir=" & IO.Path.GetDirectoryName(opd.FileName) & _
        '    '                ";driverid=1046;maxbuffersize=2048;pagetimeout=5"

        '    Dim constring = ""
        '    MsgBox(constring)
        '    Dim con = New Odbc.OdbcConnection(constring)
        '    Dim cache = New CacheManager()
        '    MsgBox("")
        '    ViewModelTableEditor.testinstance.DataSource = cache.LoadFromServer("TABLE_PLAN_REASS", con).DefaultView

        'End If
        'Dim cache = New DataManager()
        'Dim con = New SqlClient.SqlConnection("Data Source=VIVEI-10\SQLEXPRESS;Initial Catalog=CRTHISTODB;Integrated Security=True;Pooling=False")

        'Dim testtable = cache.LoadTableFromServer("HITORICALBDD", con)
        'ViewModelTableEditor.testinstance.DataSource = testtable.DefaultView
        'ViewModelTableViewer.testinstance.DataSource = testtable

        If sourceName = "estimation_reference_projection" Then
            Dim datamanager As New DataManager

            datamanager.Recalculer_Table_Reference_Projection()
        End If
    End Sub



End Class

Public Class DefaultTabSection
    Implements IViewModelTabSection

    Dim _title As String
    'Dim _IsClosable As Boolean

    Public Sub New()
        _IsClosable = False
    End Sub

    Public Property Icone As String Implements Common.UI.IViewModelTabSection.Icone
        Get

        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Property Title As String Implements Common.UI.IViewModelTabSection.Title
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property

    Public Property IsClosable As Boolean Implements IViewModelTabSection.IsClosable
End Class